package com.campus.dao.impl;

import com.campus.dao.CourseDao;
import com.campus.map.LearnUnitRowMapper;
import com.campus.model.Course;
import com.campus.model.LearnUnit;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * @desc:
 * @author: Tournesol234
 * @date: 2024/11/22 14.41
 */
@Repository
public class CourseDaoImpl implements CourseDao {

    private final JdbcTemplate jdbcTemplate;

    public CourseDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int createCourse(Course course) {
        try {
            String sql = "INSERT INTO t_manager_course (course_name, created_time, status, activity_id, teacher_id, class_id) VALUES (?, ?, ?, ?, ?, ?)";
            return jdbcTemplate.update(sql,
                    course.getCourseName(),
                    course.getCreatedTime(),
                    course.getStatus(),
                    course.getActivityId(),
                    course.getTeacherId(),
                    course.getClassId());
        } catch (Exception e) {

            System.err.println("Error occurred while creating course: " + e.getMessage());

            return 0;
        }
    }


    @Override
    public Course getCourseById(int courseId) {
        String sql = "SELECT * FROM t_manager_course WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{courseId}, (rs, rowNum) -> {
            Course course = new Course();
            course.setId(rs.getInt("id"));
            course.setCourseName(rs.getString("course_name"));
            course.setCreatedTime(rs.getTimestamp("created_time"));
            course.setStatus(rs.getString("status"));
            course.setActivityId(rs.getInt("activity_id"));
            course.setTeacherId(rs.getInt("teacher_id"));
            course.setClassId(rs.getInt("class_id"));
            return course;
        });
    }

    @Override
    public List<Course> getAllCourses() {
        String sql = "SELECT * FROM t_manager_course";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            Course course = new Course();
            course.setId(rs.getInt("id"));
            course.setCourseName(rs.getString("course_name"));
            course.setCreatedTime(rs.getTimestamp("created_time"));
            course.setStatus(rs.getString("status"));
            course.setActivityId(rs.getInt("activity_id"));
            course.setTeacherId(rs.getInt("teacher_id"));
            course.setClassId(rs.getInt("class_id"));
            return course;
        });
    }

    @Override
    public int createLearnUnit(LearnUnit learnUnit) {
        String sql = "INSERT INTO t_course_learn_unit (unit_title, doc_path, " +
                "video_path, unit_introduce, father_id, is_chpt, course_id, " +
                "learning_score) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, learnUnit.getUnitTitle(),
                learnUnit.getDocPath(), learnUnit.getVideoPath(),
                learnUnit.getUnitIntroduce(), learnUnit.getFatherId(),
                learnUnit.getIsChpt(), learnUnit.getCourseId(),
                learnUnit.getLearningScore());
    }
    @Override
    public LearnUnit getLearnUnitById(int unitId) {
        String sql = "SELECT * FROM t_course_learn_unit WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{unitId}, new LearnUnitRowMapper());
    }
    @Override
    public List<LearnUnit> getLearnUnitsByCourseId(int courseId) {
        String sql = "SELECT * FROM t_course_learn_unit WHERE course_id = ?";
        return jdbcTemplate.query(sql, new Object[]{courseId}, (rs, rowNum) -> {
            LearnUnit learnUnit = new LearnUnit();
            learnUnit.setId(rs.getInt("id"));
            learnUnit.setUnitTitle(rs.getString("unit_title"));
            learnUnit.setDocPath(rs.getString("doc_path"));
            learnUnit.setVideoPath(rs.getString("video_path"));
            learnUnit.setUnitIntroduce(rs.getString("unit_introduce"));
            learnUnit.setFatherId(rs.getInt("father_id"));
            learnUnit.setIsChpt(rs.getString("is_chpt"));
            learnUnit.setCourseId(rs.getInt("course_id"));
            learnUnit.setLearningScore(rs.getInt("learning_score"));
            return learnUnit;
        });
    }
}
